
import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
@WebListener
public class MyLstener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("会话创建了");
        int online=0;

        ServletContext context=se.getSession().getServletContext();
        if (context.getAttribute("ol")==null)
        {
            online=online+1;
            context.setAttribute("ol",online);
        }
        else
        {
            online=(int)context.getAttribute("ol");
            online=online+1;
            context.setAttribute("ol",online);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("会话销毁了");
        ServletContext context=se.getSession().getServletContext();
        int online=(int)context.getAttribute("ol");
        online=online-1;
        context.setAttribute("ol",online);
    }

}
